home *** CD-ROM | disk | FTP | other *** search
- ///--------------------------------------------------------------------------------------
- // Application.c
- ///--------------------------------------------------------------------------------------
-
- #include <Palettes.h>
- #include <SWIncludes.h>
- #include <SWGameUtils.h>
- #include <SWApplication.h>
-
- #include "Application.h"
- #include "Shark Attack.h"
- #include "NewSprite.h"
- #include "Level.h"
- #include "SWSounds.h"
- #include "MyUtils.h"
- #include "Stats.h"
- #include "Special Effects.h"
- #include "GlobalVariables.h"
-
-
- #define kMaxNumFishOnTitleScreen 4
-
-
- CTabHandle gSavedEntriesH;
-
-
- ///--------------------------------------------------------------------------------------
- // Main
- ///--------------------------------------------------------------------------------------
-
- void main( void )
- {
- OSErr err;
-
- Initialize(kNumberOfMoreMastersCalls);
- Randomize();
-
- if (SWHasSystem7())
- {
- gTempRgn = NewRgn(); // Create temp region
- AllowKeyUpEvents();
- SetCursor(*GetCursor(watchCursor));
-
- if ( !IsNewSoundManagerInstalled() )
- {
- SetCursor(&qd.arrow);
- StopAlert(130, NULL);
- ExitToShell();
- }
-
- SaveSystemVolume();
-
- err = CreateSoundChannels(4);
- FatalError(err);
- err = LoadSounds(128, kNumSounds);
- FatalError(err);
-
- CreateWindow();
- LoadPixPats();
- SetUpSpriteWorld();
- InitStats();
- SetUpWindowRegions();
- LoadSprites();
-
- CreateMenuBar();
- SetupTitleScreen();
- SetCursor(&qd.arrow);
-
- while (1)
- {
- ProcessEvents();
- AnimateTitleScreen();
- }
- }
- else
- {
- CantRunOnThisMachine();
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // ExitSharkAttack - Clean up and quit
- ///--------------------------------------------------------------------------------------
-
- void ExitSharkAttack( void )
- {
- // We don't even bother disposing this stuff, since the system will
- // clean it up anyway. They are commented out so you can see what would
- // need to be done if you did want to dispose everything yourself.
-
- /*
- SWDisposeSpriteWorld(&gSpriteWorldP);
- SWExitSpriteWorld();
- SWDisposeWindowFrame(&gStatsWindowFrameP);
- SWDisposeFrame(&gStatsBackFrameP);
- SWDisposeFrame(&gStatsNumberFrameP);
- DisposeSprites(); // This would dispose the master sprites not included in the SpriteWorld
- DisposeSounds();
- DisposeRgn(gTempRgn);
-
- DisposePixPat(gBackgroundPixPatH);
- DisposePixPat(gTitleWaterPixPatH);
- ReleaseResource((Handle)gGameWaterPictH);
- */
-
- DisposeSoundChannels();
- SWShowMenuBar(gWindowP);
- RestoreEventMask();
-
- // By hiding the window before quitting, we can avoid the brief discoloration
- // of the window caused when switching back to the system palette. Also, by
- // restoring the system palette ourselves, we can do it faster than ExitToShell.
- HideWindow(gWindowP);
- RestoreSystemPalette();
-
- RestoreSystemVolume();
-
- ExitToShell();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateMenuBar
- ///--------------------------------------------------------------------------------------
-
- void CreateMenuBar()
- {
- Handle menuBarH;
-
- menuBarH = GetNewMBar(128);
-
- if (menuBarH != NULL)
- {
- SetMenuBar(menuBarH);
- AppendResMenu(GetMenuHandle(mApple), 'DRVR');
- DrawMenuBar();
- }
- else
- {
- CantFindResource();
- }
-
- UpdateVolumeMenu();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateWindow
- ///--------------------------------------------------------------------------------------
-
- void CreateWindow( void )
- {
- gWindowP = GetNewCWindow(128, NULL, (WindowPtr)-1L);
-
- if (gWindowP != NULL)
- {
- SizeWindow(gWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(gWindowP, 0, 0, false);
- ShowWindow(gWindowP);
-
- SetPort(gWindowP);
- ForeColor(blackColor);
- BackColor(whiteColor);
- }
- else
- CantFindResource();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // LoadPixPats
- ///--------------------------------------------------------------------------------------
-
- void LoadPixPats( void )
- {
- gBackgroundPixPatH = GetPixPat(128);
- if (gBackgroundPixPatH == NULL)
- CantFindResource();
-
- gTitleWaterPixPatH = GetPixPat(129);
- if (gTitleWaterPixPatH == NULL)
- CantFindResource();
-
- gGameWaterPictH = GetPicture(129);
- if (gGameWaterPictH == NULL)
- CantFindResource();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetUpWindowRegions - calculate the gOrigWindRgn and gPixPatWindRgn
- ///--------------------------------------------------------------------------------------
-
- void SetUpWindowRegions( void )
- {
- Rect windRect;
-
- SetPort(gWindowP);
-
- gOrigWindRgn = NewRgn();
- gPixPatWindRgn = NewRgn();
- GetClip(gOrigWindRgn);
- GetClip(gPixPatWindRgn);
-
- // Subtract the SpriteWorld's windRect from the gPixPatWindRgn
- windRect = gSpriteWorldP->windRect;
- RectRgn(gPixPatWindRgn, &windRect);
- DiffRgn(gOrigWindRgn, gPixPatWindRgn, gPixPatWindRgn);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // ProcessEvents
- ///--------------------------------------------------------------------------------------
-
- void ProcessEvents( void )
- {
- EventRecord event;
-
- if ( WaitNextEvent(everyEvent, &event, kSleep, nil) )
- {
- switch ( event.what )
- {
- case mouseDown:
- HandleMouseDown( &event );
- break;
- case keyDown:
- if ( (event.modifiers & cmdKey) != 0 )
- {
- HandleMenuChoice( MenuKey(event.message & charCodeMask) );
- break;
- }
- case keyUp:
- ProcessKeyEvent(&event); // This function is in Shark Attack.c
- break;
- case updateEvt:
- UpdateWindow(&event);
- break;
- case osEvt:
- if ( ( event.message & suspendResumeMessage ) == resumeFlag )
- {
- ShowWindow(gWindowP);
- }
- else
- {
- HideWindow(gWindowP);
- RestoreSystemPalette();
- }
- break;
- }
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetupTitleScreen
- ///--------------------------------------------------------------------------------------
-
- void SetupTitleScreen( void )
- {
- gGameIsPaused = false;
-
- // Add the "Shark Attack" text to the SpriteWorld
- AddTitleSprite();
-
- // Don't copy to the screen, because we'll get an updateEvent to do that
- SWUpdateSpriteWorld(gSpriteWorldP, false);
- ShowWindow(gWindowP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AnimateTitleScreen
- ///--------------------------------------------------------------------------------------
-
- void AnimateTitleScreen( void )
- {
- SWProcessSpriteWorld(gSpriteWorldP);
- SWAnimateSpriteWorld(gSpriteWorldP);
-
- // Add fish periodically
- if (gSpriteWorldP->frameHasOccurred)
- {
- if (gNumFishOnScreen < kMaxNumFishOnTitleScreen)
- {
- if (gFishDelay > 0)
- {
- gFishDelay--;
- }
- else if (GetRandom(0, kFishRandomness) == 0)
- {
- gFishDelay = kMinNewFishDelay;
- AddFish();
- }
- }
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // HandleMouseDown
- ///--------------------------------------------------------------------------------------
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr whichWindow;
- short int thePart;
- long menuChoice;
-
- thePart = FindWindow(eventPtr->where, &whichWindow);
-
- switch (thePart)
- {
- case inMenuBar:
- menuChoice = MenuSelect(eventPtr->where);
- HandleMenuChoice(menuChoice);
- break;
- case inSysWindow:
- SystemClick(eventPtr, whichWindow);
- break;
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // UpdateWindow
- ///--------------------------------------------------------------------------------------
-
- void UpdateWindow( EventRecord *eventPtr )
- {
- if ( (WindowPtr)eventPtr->message == gWindowP )
- {
- BeginUpdate(gWindowP);
- SetPort(gWindowP);
-
- // Draw the background pattern
- SetClip(gPixPatWindRgn);
- FillCRect(&gWindowP->portRect, gBackgroundPixPatH);
- SetClip(gOrigWindRgn);
-
- // Update the SpriteWorld's worldRect
- SWUpdateWindow(gSpriteWorldP);
-
- // Update stats area if a game is paused
- if (gGameIsPaused)
- UpdateStatsArea();
-
- EndUpdate(gWindowP);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // HandleMenuChoice
- ///--------------------------------------------------------------------------------------
-
- void HandleMenuChoice( long menuChoice )
- {
- short menu;
- short item;
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch ( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- case mVolume:
- HandleVolumeChoice( item);
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // HandleAppleChoice
- ///--------------------------------------------------------------------------------------
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
- OSErr err;
-
- switch ( item )
- {
- case iAbout:
- err = DoAboutBox();
- FatalError(err);
- break;
- default:
- appleMenu = GetMenuHandle(mApple);
- GetMenuItemText(appleMenu, item, accName);
- accNumber = OpenDeskAcc(accName);
- break;
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // HandleFileChoice
- ///--------------------------------------------------------------------------------------
-
- void HandleFileChoice(short item)
- {
- Boolean stereoIsOn;
- short markChar;
-
- switch ( item )
- {
- case iNewGame:
- NewGame();
- break;
- case iEndGame:
- gGameOver = true;
- gGameIsPaused = false;
- break;
- case iStereoSound:
- GetItemMark(GetMenuHandle(mFile), iStereoSound, &markChar);
- stereoIsOn = (markChar != checkMark);
- CheckItem(GetMenuHandle(mFile), iStereoSound, stereoIsOn);
- SetStereoMode(stereoIsOn);
- break;
- case iPauseGame:
- gGameIsPaused = false;
- break;
- case iQuit:
- ExitSharkAttack();
- break;
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // HandleVolumeChoice
- ///--------------------------------------------------------------------------------------
-
- void HandleVolumeChoice(short menuItem)
- {
- SetSystemVolume(menuItem-1);
- PlaySound(kHitFishSnd, 2, kReplaceSameSound);
- UpdateVolumeMenu();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // UpdateVolumeMenu - place the check mark by the appropriate item
- ///--------------------------------------------------------------------------------------
-
- void UpdateVolumeMenu( void )
- {
- short volume, menuItem, n;
-
- // Get the current system volume
- GetSystemVolume(&volume);
- menuItem = volume+1;
-
- // Place the check by the corresponding item
- for (n = 0; n <= 8; n++)
- CheckItem(GetMenuHandle(mVolume), n, (menuItem == n) );
- }
-
-
- ///--------------------------------------------------------------------------------------
- // DoAboutBox
- ///--------------------------------------------------------------------------------------
-
- OSErr DoAboutBox(void)
- {
- PicHandle aboutPictH;
- WindowPtr aboutWindP;
- OSErr err = noErr;
-
- aboutPictH = GetPicture(kAboutPictResID);
- if (aboutPictH == NULL)
- err = ResError() ? ResError() : resNotFound;
-
- if (err == noErr)
- {
- aboutWindP = GetNewCWindow(kAboutWindResID, NULL, (WindowPtr)-1L);
- if (aboutWindP == NULL)
- err = ResError() ? ResError() : resNotFound;
- }
-
- if (err == noErr)
- {
- PlaySound(kFishDeadSnd, 2, kFindEmptyChannel);
-
- SetWindowPic(aboutWindP, aboutPictH);
- ShowWindow(aboutWindP);
-
- AboutBoxEventLoop();
-
- // First we clear the picture from the window
- SetWindowPic(aboutWindP, NULL);
- // Then we dispose it through the resource manager
- ReleaseResource((Handle)aboutPictH);
- // Finally we can dispose the window with no effect on the windowPic
- DisposeWindow(aboutWindP);
- }
-
- return err;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AboutBoxEventLoop
- ///--------------------------------------------------------------------------------------
-
- void AboutBoxEventLoop( void )
- {
- EventRecord event;
- Boolean done = false;
-
- do
- {
- if ( WaitNextEvent(everyEvent, &event, kSleep, nil) )
- {
- switch ( event.what )
- {
- case mouseDown:
- done = true;
- break;
- case updateEvt: // Update the window behind the About Box.
- UpdateWindow(&event); // The About Box will update automatically,
- break; // since it has a windowPic installed.
- }
- }
- else
- {
- if (!gGameIsPaused) // Run animation if the game hasn't started yet
- AnimateTitleScreen();
- }
- } while (!done);
- }
-
-